export default class DataBase {
    private static dataBaseContext: { [id: string]: any } = {}

    /**
     * base
     * @param type
     * @param handle
     */
    public static getData(type: string, handle: handle): any {
        if (!IsHandle(handle)) {
            let tb = debug.traceback();
            log.error("DataBase.getData():" + type + ":你传的handle为空或0！")
            log.error(tb)
            return null;
        }
        let key = type + GetHandleId(handle)
        let data = this.dataBaseContext[key]
        if (!data) {
            data = {}
            this.dataBaseContext[key] = data
        }
        return data;
    }

    public static setData(type: string, handle: handle, obj: any): void {
        if (!IsHandle(handle)) {
            let tb = debug.traceback();
            log.error("DataBase.setData():" + type + ":你传的handle为空或0！")
            log.error(tb)
            return null;
        }
        let key = type + GetHandleId(handle)
        if (obj == null && this.dataBaseContext[key] == null) {
            //如果有才清空 没有就不要设置一个key为null了
            return
        }
        this.dataBaseContext[key] = obj
    }

    public static clearData(type: string, handle: handle): any {
        if (!IsHandle(handle)) {
            let tb = debug.traceback();
            log.error("DataBase.clearData():" + type + ":你传的handle为空或0！")
            log.error(tb)
            return null;
        }
        let key = type + GetHandleId(handle)
        let data = this.dataBaseContext[key]
        if (data) {//如果有才清空 没有就不要设置一个key为null了
            this.dataBaseContext[key] = null
        }
    }

    /**
     * addons
     */
    public static getUnitSolarData(handle: unit): { [key: string]: any } {
        return DataBase.getData("Unit", handle)
    }

    public static clearUnitSolarData(handle: unit) {
        DataBase.clearData("Unit", handle)
    }

    public static getItemSolarData(handle: item): { [key: string]: any } {
        return DataBase.getData("Item", handle)
    }

    public static clearItemSolarData(handle: item) {
        DataBase.clearData("Item", handle)
    }

    public static getDialogButtonSolarData(handle: button): { [key: string]: any } {
        return DataBase.getData("DialogButton", handle)
    }

    public static clearDialogButtonSolarData(handle: button) {
        DataBase.clearData("DialogButton", handle)
    }

    public static getPlayerSolarData(handle: player): { [key: string]: any } {
        return DataBase.getData("Player", handle)
    }

    public static clearPlayerSolarData(handle: player) {
        DataBase.clearData("Player", handle)
    }


}